package com.example.controller;

import com.example.Repository.VisiterRepository;
import com.example.domain.property.User;
import com.example.domain.visit.Visiter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/visiter")
public class VisiterController {
    @Autowired
    private VisiterRepository visiterRepository;
    //登录
    @PostMapping("/login")
    @ResponseBody
    public Visiter login(@RequestBody Visiter Visiter, HttpSession session){
        Visiter res=visiterRepository.findByUsernameAndPassword(Visiter.getUsername(),Visiter.getPassword());
        if(res!=null){
            res.setPassword(null);
            session.setAttribute("visiter",res);
        }
        return res;
    }
    //首页获取数据用户数据
    @GetMapping("/gainData")
    @ResponseBody
    public Visiter gainData(HttpSession session){
        Visiter visiter=(Visiter)session.getAttribute("visiter");
        return visiter;
    }
    //注销
    @GetMapping("/logout")
    @ResponseBody
    public Boolean logout(HttpSession session){
        session.removeAttribute("visiter");
        return true;
    }
}
